# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: Krassy Boykinov <kboykinov@teamcentrixx.com>
pkgname=mozjs115
pkgver=115.10.0
pkgrel=0
pkgdesc="Standalone Mozilla JavaScript engine (115 ESR)"
url="https://spidermonkey.dev/"
# s390x: no lld
# armhf: unsupported assembly
arch="all !armhf !s390x"
license="MPL-2.0"
depends_dev="
	$pkgname
	icu-dev
	libffi-dev
	nspr-dev
	"
_llvmver=17
makedepends="
	$depends_dev
	cargo
	clang$_llvmver
	linux-headers
	llvm$_llvmver-dev
	m4
	perl
	python3
	rust
	sed
	zlib-dev
	"
checkdepends="icu-data-full"
subpackages="$pkgname-dev"

source="https://ftp.mozilla.org/pub/firefox/releases/${pkgver}esr/source/firefox-${pkgver}esr.source.tar.xz
	disable-moz-stackwalk.patch
	fix-musl-build.patch
	fix-rust-target.patch
	python-deps.patch
	"
builddir="$srcdir"/firefox-$pkgver

# broken on riscv64
case "$CARCH" in
riscv64)
	_linker="bfd"
	;;
*)
	makedepends="$makedepends lld"
	_linker="lld"
	;;
esac

prepare() {
	default_prepare
	mkdir "$srcdir/mozbuild"

	cat > .mozconfig <<- END
	ac_add_options --enable-application=js
	mk_add_options MOZ_OBJDIR='${PWD}'/obj
	ac_add_options --prefix=/usr
	ac_add_options --enable-release
	ac_add_options --enable-hardening
	ac_add_options --enable-optimize="$CFLAGS -O2"
	ac_add_options --enable-rust-simd
	ac_add_options --enable-linker=$_linker
	ac_add_options --disable-bootstrap
	ac_add_options --disable-debug
	ac_add_options --disable-debug-symbols
	ac_add_options --disable-jemalloc
	ac_add_options --disable-strip

	# System libraries
	ac_add_options --with-system-icu
	ac_add_options --with-system-nspr
	ac_add_options --with-system-zlib

	# Features
	ac_add_options --enable-shared-js
	ac_add_options --enable-tests
	ac_add_options --with-intl-api
	mk_add_options RUSTFLAGS="$RUSTFLAGS"
	END
}

# secfixes:
#   115.9.1-r0:
#     - CVE-2024-29944
#   115.8.0-r0:
#     - CVE-2024-1553
#   115.6.0-r0:
#     - CVE-2023-6864
#   115.4.0-r0:
#     - CVE-2023-5728

build() {
	# fixed next major cycle
	unset RUSTFLAGS
	export LDFLAGS="$LDFLAGS -Wl,-z,stack-size=1048576"

	# FF doesn't have SIMD available on armhf/v7
	case "$CARCH" in
	arm*)
		echo 'ac_add_options --disable-rust-simd' >> .mozconfig
		;;
	*)
		echo 'ac_add_options --enable-rust-simd' >> .mozconfig
		;;
	esac

	unset CARGO_PROFILE_RELEASE_OPT_LEVEL
	unset CARGO_PROFILE_RELEASE_LTO
	export CC=clang
	export CXX=clang++
	export CFLAGS="$CFLAGS -O2"
	export CXXFLAGS="$CXXFLAGS -O2"
	export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
	export MOZBUILD_STATE_PATH="$srcdir/mozbuild"
	export MOZ_NOSPAM=1
	export PYTHON=/usr/bin/python3
	export RUST_TARGET="$CTARGET"
	export SHELL=/bin/ash
	export RUSTFLAGS="$RUSTFLAGS -C debuginfo=1"

	./mach build
}

check() {
	obj/dist/bin/jsapi-tests \
		--format=none \
		--exclude-random \
		basic
}

package() {
	cd obj
	make DESTDIR="$pkgdir" install
	rm -f "$pkgdir"/usr/lib/*.ajs
}

sha512sums="
0626e2c68ce43f24dfc2b9216e2565537ad8781daf4195d53420e1b78d57d0f6360fbe56b0ddbedae3818546c72472c85c1ff2b208c123d32a0543e666f42b65  firefox-115.10.0esr.source.tar.xz
454ea3263cabce099accbdc47aaf83be26a19f8b5a4568c01a7ef0384601cf8315efd86cd917f9c8bf419c2c845db89a905f3ff9a8eb0c8e41042e93aa96a85c  disable-moz-stackwalk.patch
bc91c2fb15eb22acb8acc36d086fb18fbf6f202b4511d138769b5ecaaed4a673349c55f808270c762616fafa42e3b01e74dc0af1dcbeea1289e043926e2750c8  fix-musl-build.patch
0b222a36cf3fd446dfb7fd171d77c58c0f19bc4df8f61cb2d99f2dfa0998a24e4d11a92fb88a1144b1bdd8ace95a858dca0a5e9ad5a4ea37a13b2cb7c245758a  fix-rust-target.patch
4e40b34c5f77a1a21fe971a6fcd8a21b1a63423a3a7932a5a6e1c7a2779f9f06a561c806614a01931679a4b1c6afdfd8ae1f3cc6b673f259ccd368e8e54f6e90  python-deps.patch
"
